// pages/mys/video/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 列表数据
    dtotal: 0,
    dlist: [],
    // 播放弹窗
    isPop: 0, //是否弹出
    popLink: '', //播放链接
    popTitle: '', //播放标题
    pageStart: 1,
    pageLimit: 4,
    pageStr: '1/2',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 返回
  toBack: function (e) {
    console.log(e);
    wx.navigateBack({
      delta: 1
    }); return
  },

  // 获取数据
  getList: function (e) {
    var that = this;
    var formData = {
        start: this.data.pageStart,
        limit: this.data.pageLimit,
    };
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/my/video/list',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        var d = res.data.lists;
        var total = res.data.total;
        that.setData({
            dtotal: total,
            dlist: d,
        });
        if (d.length >= that.data.pageLimit) {
            that.setData({
                pageStart: that.data.pageStart * 1 + 1,
            });
        }
        // console.log(res.data);
        // var dtotal = res.data.total;
        // var dlist = res.data.lists;
        // var total = that.data.dtotal;
        // var lists = that.data.dlist;
        // var totalNew = total*1 + dtotal;
        // var listNew = lists;
        // for (var i = 0; i < dlist.length; i ++) {
        //     listNew.push(dlist[i]);
        // }
        // that.setData({
        //     dtotal: totalNew,
        //     dlist: listNew,
        // });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 弹窗播放
  toPlay: function (e) {
    // console.log(e);
    var link = e.currentTarget.dataset.link;
    var title = e.currentTarget.dataset.title;
    // console.log(link);
    if (!link) { return }
      this.setData({
          popLink: link,
          popTitle: title,
          isPop: 1,
      });
  },

  // 关闭弹窗
  popClose: function (e) {
      console.log(e);
      this.setData({
          popLink: '',
          isPop: 0,
      });
  },

  // 首页
  toFirst: function (e) {
    this.setData({
        pageStart: 1,
    });
    this.getList();
    // wx.reLaunch({
    //   url: '/pages/mys/video/index',
    // })
  },

  // 下一页
  toNext: function (e) {
    this.getList();
  },
})